package com.huiw.archives.modules.sys.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huiw.archives.common.utils.R;
import com.huiw.archives.modules.sys.entity.WisArchivesPerson;
import com.huiw.archives.modules.sys.entity.WisArchivesTask;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 登录相关
 * 
 * @author yangc
 * @email yangcheng@huiwtech.com
 * @date 2016年11月10日 下午1:15:31
 */
@RestController
@RequestMapping("main")
public class BussController extends BaseControl {

	/**
	 * 总数统计
	 * 
	 * @throws ParseException
	 */
	@RequestMapping(value = "/taskAll")
	public R taskAll() throws Exception {

		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(nowDate);
		String nowDateStr = dft.format(date.getTime());
		date.set(Calendar.DATE, date.get(Calendar.DATE) - 5);
		String lessFiveDateStr = dft.format(date.getTime());
		// 办结时间不足5天
		Integer lessFive = wisArchivesTaskService.queryByHql("select sysSeq FROM WisArchivesTask WHERE taskDealTimeEnd >='"
				+ lessFiveDateStr + "' and taskDealTimeEnd <='" + nowDateStr + "' and taskStatus<" + 4).size();

		// 已超期件数
		Integer overTime = wisArchivesTaskService.queryByHql(
				"select sysSeq FROM WisArchivesTask WHERE taskDealTimeEnd >='" + nowDateStr + "' and taskStatus<" + 4)
				.size();

		// 待办总数
		Integer waitingAll = (int) wisArchivesTaskService.queryRecordNum(new WisArchivesTask(), "taskStatus", 1, "=");

		// 已办总数
		Integer completeAll = (int) wisArchivesTaskService.queryRecordNum(new WisArchivesTask(), "taskStatus", 4, "=");

		return R.ok().put("lessFive", lessFive).put("overTime", overTime).put("waitingAll", waitingAll)
				.put("completeAll", completeAll);

	}

	/**
	 * 本月指标
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/taskByMonth")
	public R taskByMonth() throws Exception {

		SimpleDateFormat Y_M = new SimpleDateFormat("yyyy-MM");
		Date nowDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(nowDate);

		// 新增数
		String nowDateStr = Y_M.format(date.getTime());
		Integer increaseNums = wisArchivesTaskService.queryByHql(
				"select sysSeq FROM WisArchivesTask WHERE taskReceiveTime like '" + nowDateStr + "%' and taskStatus=" + 1)
				.size();

		// 已办理数
		Integer answeredNums = (int) wisArchivesTaskService.queryRecordNumByAND(new WisArchivesTask(),
				new String[] { "taskReceiveTime", "taskStatus" }, new Object[] { nowDateStr + "%", 4 },
				new String[] { "like", "=" });

		// 正在办理数
		Integer dealingNums = (int) wisArchivesTaskService.queryRecordNumByAND(new WisArchivesTask(),
				new String[] { "taskReceiveTime", "taskStatus", "taskStatus" }, new Object[] { nowDateStr + "%", 1, 4 },
				new String[] { "like", ">", "<" });

		return R.ok().put("increaseNums", increaseNums).put("answeredNums", answeredNums).put("dealingNums",
				dealingNums);

	}

	/**
	 * 群众满意度
	 */
	@RequestMapping(value = "/satisfaction")
	public R satisfaction() throws Exception {
		JSONObject json = new JSONObject();
		JSONArray ja = new JSONArray();

		// 基本满意
		Integer satisfaction_one = (int) wisArchivesTaskService.queryRecordNumByAND(new WisArchivesTask(),
				new String[] { "taskReflectSatisfac", "taskReflectSatisfac" }, new Object[] { 1, 4 },
				new String[] { ">", "<" });
		json.put("name", "基本满意");
		json.put("value", satisfaction_one);
		ja.add(json);
		// 不满意
		Integer satisfaction_two = (int) wisArchivesTaskService.queryRecordNumByAND(new WisArchivesTask(),
				new String[] { "taskReflectSatisfac" }, new Object[] { 2 }, new String[] { "<=" });
		json.put("name", "不满意");
		json.put("value", satisfaction_two);
		ja.add(json);
		// 满意
		Integer satisfaction_three = (int) wisArchivesTaskService.queryRecordNumByAND(new WisArchivesTask(),
				new String[] { "taskReflectSatisfac" }, new Object[] { 3 }, new String[] { ">=", "<" });
		json.put("name", "满意");
		json.put("value", satisfaction_three);

		ja.add(json);
		return R.ok().put("data", ja);

	}

	/**
	 * 人口流动
	 */
	@RequestMapping(value = "/population")
	public R population() throws Exception {
		JSONObject json = new JSONObject();
		JSONArray ja = new JSONArray();

		// 常住人口
		Integer population_one = (int) wisArchivesPersonService.queryRecordNum(new WisArchivesPerson(),
				"personWorkSituation", null, "=");
		json.put("name", "常住人口");
		json.put("value", population_one);
		ja.add(json);
		// 外出人口
		Integer population_All = (int) wisArchivesPersonService.queryRecordNum(new WisArchivesPerson(), "", "");
		json.put("name", "外出人口");
		json.put("value", population_All - population_one);
		ja.add(json);

		return R.ok().put("data", ja);

	}

	/**
	 * 贫困情况
	 */
	@RequestMapping(value = "/poverty")
	public R poverty() throws Exception {
		JSONObject json = new JSONObject();
		JSONArray series_data_one = new JSONArray();
		JSONArray series_data_two = new JSONArray();

		// // 总人口
		Integer poverty_one = (int) wisArchivesPersonService.queryRecordNum(new WisArchivesPerson(), "", "");
		// 脱 贫人口
		Integer poverty_two = (int) wisArchivesPersonService.queryRecordNum(new WisArchivesPerson(),
				"personFamilySituation", null, "=");
		json.put("value", poverty_two);
		json.put("name", "脱贫人口");

		series_data_one.add(json);
		// 贫困人口
		json.put("value", poverty_one - poverty_two);
		json.put("name", "贫困人口");

		series_data_one.add(json);
		// 总户数
		Integer poverty_three = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personFamilyRelation" }, new Object[] { "户主" }, new String[] { "=" });
		// 脱贫户数
		Integer poverty_four = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personFamilyRelation", "personFamilySituation" }, new Object[] { "户主", null },
				new String[] { "=", "=" });
		json.put("value", poverty_four);
		json.put("name", "脱贫户数");

		series_data_two.add(json);
		// 贫困户数
		json.put("value", poverty_three - poverty_four);
		json.put("name", "贫困户数");

		series_data_two.add(json);

		return R.ok().put("series_data_one", series_data_one).put("series_data_two", series_data_two);

	}

	/**
	 * 年龄状态
	 */
	@RequestMapping(value = "/age")
	public R age() throws Exception {
		JSONObject json = new JSONObject();
		JSONArray ja = new JSONArray();

		// 0-6岁
		Integer age_one = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personAge", "personAge" }, new Object[] { 0, 6 }, new String[] { ">=", "<" });
		json.put("name", "0-6岁");
		json.put("value", age_one);
		ja.add(json);
		// 7-17岁
		Integer age_two = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personAge", "personAge" }, new Object[] { 7, 17 }, new String[] { ">=", "<" });
		json.put("name", "7-17岁");
		json.put("value", age_two);
		ja.add(json);
		// 18-59岁
		Integer age_three = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personAge", "personAge" }, new Object[] { 18, 59 }, new String[] { ">=", "<" });
		json.put("name", "18-59岁");
		json.put("value", age_three);
		ja.add(json);
		// 60-80岁
		Integer age_four = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personAge", "personAge" }, new Object[] { 60, 80 }, new String[] { ">=", "<" });
		json.put("name", "60-80岁");
		json.put("value", age_four);
		ja.add(json);
		// 80岁以上
		Integer age_five = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personAge" }, new Object[] { 80 }, new String[] { ">=" });
		json.put("name", "80岁以上");
		json.put("value", age_five);
		ja.add(json);

		return R.ok().put("data", ja);

	}

	/**
	 * 学历
	 */
	@RequestMapping(value = "/education")
	public R education() throws Exception {
		String[] xAxis_data = new String[] { "大学以上", "高中学历", "初中学历", "小学学历", "失学儿童", "文盲" };
		Integer[] series_data = new Integer[6];

		// 大学以上
		Integer education_one = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personEducation" }, new Object[] { 4 }, new String[] { ">=" });
		series_data[0] = education_one;
		// 高中学历
		Integer education_two = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personEducation" }, new Object[] { 3 }, new String[] { "=" });
		series_data[1] = education_two;
		// 初中学历
		Integer education_three = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personEducation" }, new Object[] { 2 }, new String[] { "=" });
		series_data[2] = education_three;
		// 小学学历
		Integer education_four = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personEducation" }, new Object[] { 1 }, new String[] { "=" });
		series_data[3] = education_four;
		// 失学儿童
		Integer education_five = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personEducation", "personAge", "personAge" }, new Object[] { 0, 6, 18 },
				new String[] { "=", ">=", "<" });
		series_data[4] = education_five;
		// 文盲
		Integer education_six = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personEducation" }, new Object[] { 0 }, new String[] { "=" });
		series_data[5] = education_six;

		return R.ok().put("xAxis_data", xAxis_data).put("series_data", series_data);

	}

	/**
	 * 婚姻状态
	 */
	@RequestMapping(value = "/marry")
	public R marry() throws Exception {
		JSONObject json = new JSONObject();
		JSONArray ja = new JSONArray();

		// 已婚
		Integer marry_one = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personMarry" }, new Object[] { 0 }, new String[] { ">" });
		json.put("name", "已婚");
		json.put("value", marry_one);
		ja.add(json);
		// 未婚
		Integer marry_two = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personMarry" }, new Object[] { 0 }, new String[] { "=" });
		json.put("name", "未婚");
		json.put("value", marry_two);
		ja.add(json);
		// 年满30未婚
		Integer marry_three = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personMarry", "personAge" }, new Object[] { 0, 30 }, new String[] { "=", ">" });
		json.put("name", "年满30未婚");
		json.put("value", marry_three);
		ja.add(json);

		return R.ok().put("data", ja);

	}

	/**
	 * 男女比例
	 */
	@RequestMapping(value = "/sex")
	public R sex() throws Exception {
		JSONObject json = new JSONObject();
		JSONArray ja = new JSONArray();

		// 男
		Integer sex_one = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personSex" }, new Object[] { "男" }, new String[] { "=" });
		json.put("name", "男");
		json.put("value", sex_one);
		ja.add(json);
		// 女
		Integer sex_two = (int) wisArchivesPersonService.queryRecordNumByAND(new WisArchivesPerson(),
				new String[] { "personSex" }, new Object[] { "女" }, new String[] { "=" });
		json.put("name", "女");
		json.put("value", sex_two);
		ja.add(json);

		return R.ok().put("data", ja);

	}

}
